package com.qinggeng.project.system.controller;

import java.util.List;

import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.project.system.domain.RegionalAgency;
import com.qinggeng.project.system.service.IRegionalAgencyService;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.web.page.TableDataInfo;
/**
 * 【请填写功能名称】Controller
 *
 * @author qinggeng
 * @date 2020-11-28
 */
@RestController
@RequestMapping("/system/agency")
public class RegionalAgencyController extends BaseController
{
    @Autowired
    private IRegionalAgencyService regionalAgencyService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:agency:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegionalAgency regionalAgency)
    {
        startPage();
        List<RegionalAgency> list = regionalAgencyService.listRegionalAgency(regionalAgency);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:agency:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RegionalAgency regionalAgency)
    {
        List<RegionalAgency> list = regionalAgencyService.listRegionalAgency(regionalAgency);
        ExcelUtil<RegionalAgency> util = new ExcelUtil<RegionalAgency>(RegionalAgency.class);
        return util.exportExcel(list, "agency");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:agency:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(regionalAgencyService.getRegionalAgencyById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:agency:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegionalAgency regionalAgency)
    {
        return toAjax(regionalAgencyService.insertRegionalAgency(regionalAgency));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:agency:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegionalAgency regionalAgency)
    {
        return toAjax(regionalAgencyService.updateRegionalAgency(regionalAgency));
    }

    /**
     * 处理区域代理信息
     */
    @PreAuthorize("@ss.hasPermi('system:agency:edit')")
    @Log(title = "代理信息", businessType = BusinessType.UPDATE)
    @PostMapping("/deal")
    public AjaxResult deal(@RequestBody RegionalAgency regionalAgency)
    {
        if(!regionalAgency.getStatus().equals(CommonEnum.STATUS_10.getValue())){
            return AjaxResult.error("该区域代理信息已被处理过，请勿重复处理！", ShowTypeEnum.用户.getValue());
        }
        regionalAgency.setStatus(CommonEnum.STATUS_20.getValue());
        return toAjax(regionalAgencyService.updateRegionalAgency(regionalAgency));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:agency:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(regionalAgencyService.deleteRegionalAgencyByIds(ids));
    }
}
